package zhtt.example.config;/*
package zhtt.example.config;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import zhtt.example.service.LoadBalancerService;

@Configuration
public class CustomGatewayFilterConfig {

    @Bean
    public GlobalFilter customGlobalFilter(LoadBalancerService loadBalancerService) {
        return (exchange, chain) -> {
            // 从请求中提取服务名称
            String serviceName = exchange.getRequest().getQueryParams().getFirst("service");

            if (serviceName != null) {
                // 获取服务实例
                Mono<ServiceInstance> instanceMono = loadBalancerService.getServiceInstance(serviceName);

                // 使用服务实例进行进一步处理
                return instanceMono.flatMap(instance -> {
                    // 例如，将服务实例的信息添加到响应头
                    exchange.getResponse().getHeaders().add("X-Service-Host", instance.getHost());
                    exchange.getResponse().getHeaders().add("X-Service-Port", String.valueOf(instance.getPort()));
                    return chain.filter(exchange);
                });
            } else {
                return chain.filter(exchange);
            }
        };
    }
}
*/
